package com.cqut.hfh.ordering.controller;

import com.cqut.hfh.ordering.pojo.EhyManager;
import com.cqut.hfh.ordering.pojo.EhyModule;
import com.cqut.hfh.ordering.pojo.EhyRole;
import com.cqut.hfh.ordering.service.IManagerService;
import com.cqut.hfh.ordering.service.IModuleService;
import com.cqut.hfh.ordering.service.IRoleService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/22-19:50
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Resource
    private IManagerService managerService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IModuleService moduleService;

    /**
     * 登录
     * @param man_user
     * @param man_pwd
     * @param request
     * @return
     */
    @RequestMapping(path = "/loginSubmit", method = RequestMethod.POST)
    public String login(@RequestParam("manUser") String man_user,
                        @RequestParam("manPwd") String man_pwd,
                        HttpServletRequest request){

        EhyManager ehyManager = managerService.login(man_user, man_pwd);

        //失败
        if(ehyManager == null){
            request.setAttribute("mes", "用户名或密码错误！");
            return "wzw/login";
        }

        //成功
        request.getSession().setAttribute("manager", ehyManager);
        request.setAttribute("userName", ehyManager.getMan_user());

        
        //处理重复module

        List<EhyRole> roles_list = roleService.getRolesById(ehyManager.getMan_id());
        List<EhyModule> modules_List = new ArrayList<>();

        for (EhyRole e : roles_list) {

            List<EhyModule> modules_temp = moduleService.getModulesById(e.getRole_id());

            if(modules_List.size() != 0) {
                for (EhyModule m : modules_List) {

                    for (EhyModule m1 : modules_temp) {

                        if (!m.getMod_id().equals(m1.getMod_id())) {
                            modules_List.add(m);
                        } else {
                            break;
                        }
                    }
                }
            }else {
                modules_List = modules_temp;
            }

        }

        request.setAttribute("list", modules_List);
        return "wzw/index";
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping("/logout")
    public String logout(){

        return "wzw/login";
    }

    /**
     * 添加管理员，并设置管理员对应的角色
     * @param manager
     * @param role_ids
     * @return
     */
    @RequestMapping(path = "/manAdd",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String manAdd(EhyManager manager, @RequestParam("str") String role_ids){

        manager.setOptime(new Date());

        if( manager.getMan_id() == null) {
            manager.setMan_id(EntityIdutils.getId());
            int temp = managerService.saveManage(manager, role_ids);
            return Message.getReturnMsg(0, "");
        }
//        }else{
//            int temp = managerService.updateManage(manager, role_ids);
//        }
        return "";
    }

    /**
     * 修改管理员信息
     * @param manager
     * @param role_ids
     * @return
     */
    @RequestMapping(path = "/manUpdate",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String manUpdate(EhyManager manager, @RequestParam("str") String role_ids){
        manager.setOptime(new Date());

        if( manager.getMan_id() != null) {
            int temp = managerService.updateManage(manager, role_ids);
        }
        return "";
    }

    /**
     * 获取所有的管理员
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findAlls",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findAlls(@RequestParam("limit") String limit,
                           @RequestParam("page") String page){
        List<Map<String, Object>> manage_list = managerService.getManage(limit, page);
        for( Map<String, Object> map : manage_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }
        Integer count = managerService.getCount();
        return Message.getReturnMsg(0,"", count, manage_list);
    }

    /**
     * 通过id删除管理员以及管理员角色表
     * @param id
     * @return
     */
    @RequestMapping(path = "/manDelete", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String deleteById(@RequestParam("id") String id){
        int temp = managerService.delById(id);
        if( temp == 1){
            return "{\"msg\":\"ok\"}";
        }
        return "{\"msg\":\"fail\"}";
    }

    /**
     * 通过id查找管理员
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public EhyManager findById(String id){
        Map<String, Object> result_map = managerService.findById(id);
        EhyManager e = new EhyManager();
        e.setProperties(result_map);
        return e;
    }

    /**
     * 通过管理员id获取角色
     * @param manId
     * @return
     */
    @RequestMapping("/findManagerRole")
    @ResponseBody
    public List<EhyRole> findManagerRole(@RequestParam("manId")String manId){
        List<EhyRole> result_map = roleService.getRolesById(manId);
        return result_map;
    }

    /**
     * 通过管理员名称查询管理员信息
     * @param limit
     * @param page
     * @param man_user
     * @return
     */
    @RequestMapping(path = "/findManagersByName",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findManagersByName(@RequestParam("limit") String limit,
                                     @RequestParam("page") String page,
                                     @RequestParam("man_user") String man_user){

        List<Map<String, Object>> managers_list = managerService.getManagersByName(limit, page, man_user);

        for( Map<String, Object> map : managers_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = managerService.getCount();

        return Message.getReturnMsg(0,"", count, managers_list);
    }






}
